package me.arraylist.Test;

import me.demo.User;

import java.util.ArrayList;
import java.util.Scanner;

public class UserTest {
    public static void main(String[] args) {
        ArrayList<User> list = new ArrayList<>();
        User u1 = new User("a1", "zhangsan", "123");
        User u2 = new User("b2", "lisi", "123");
        User u3 = new User("c3", "wangwu", "123");
        list.add(u1);
        list.add(u2);
        list.add(u3);
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查找的用户id");
        String id = sc.next();
        boolean flag = find(id, list);
        int index = getIndex(id, list);
        if (flag) {
            System.out.println(index);
        } else {
            System.out.println("用户不存在");
        }
    }

    public static int getIndex(String id, ArrayList<User> list) {
        for (int i = 0; i < list.size(); i++) {
            if (id.equals(list.get(i).getId())) {
                return i;
            }
        }
        return -1;
    }

    public static boolean find(String id, ArrayList<User> list) {
        return getIndex(id, list) >= 0;
    }
}
